# 使用多阶段构建减小镜像大小
FROM maven:3.8.6-openjdk-11 AS build

# 1. 设置安全协议
RUN mkdir -p /etc/ssl/java && \
    echo "TLSv1.2" > /etc/ssl/java/custom.security && \
    echo "TLSv1.3" >> /etc/ssl/java/custom.security

# 2. 配置Maven镜像源 - 使用腾讯云镜像
RUN mkdir -p /root/.m2 && \
    echo '<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd"><mirrors><mirror><id>tencent</id><mirrorOf>*</mirrorOf><name>Tencent Mirror</name><url>https://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url></mirror></mirrors></settings>' > /root/.m2/settings.xml

# 3. 设置工作目录
WORKDIR /workspace

# 4. 复制整个项目（包括父POM）
COPY . .

# 5. 构建todo-service模块（跳过测试）
RUN mvn clean package -pl todo-service -am -DskipTests

# 6. 运行阶段
FROM openjdk:11-jre-slim

# 7. 设置时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# 8. 设置工作目录
WORKDIR /app

# 9. 复制JAR文件
COPY --from=build /workspace/todo-service/target/todo-service-*.jar app.jar

# 10. 设置环境变量
ENV TZ=Asia/Shanghai \
    SPRING_PROFILES_ACTIVE=default \
    SPRING_CLOUD_NACOS_DISCOVERY_SERVER_ADDR=nacos-server.todo-system.svc.cluster.local:8848 \
    SPRING_CLOUD_NACOS_CONFIG_SERVER_ADDR=nacos-server.todo-system.svc.cluster.local:8848 \
    SERVER_PORT=8081

# 11. 暴露端口
EXPOSE 8081

# 12. 健康检查
HEALTHCHECK --interval=30s --timeout=3s \
  CMD curl -f http://localhost:8081/actuator/health || exit 1

# 13. 启动应用
ENTRYPOINT ["java", "-jar", "app.jar"]